package com.sanley.piss.web.service;


import com.sanley.piss.mapper.DictionaryDataMapper;
import com.sanley.piss.mapper.FamilyMapper;
import com.sanley.piss.model.ServiceImpl;
import com.sanley.piss.model.StringUtil;
import com.sanley.piss.model.exceptionUtil.exception.ServiceException;
import com.sanley.piss.model.exceptionUtil.json.ResultEnum;
import com.sanley.piss.model.po.Family;
import com.sanley.piss.model.vo.FamilyVO;
import com.sanley.piss.web.service.mapper.IFamilyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * Create By  on 2021/3/13.
 *
 * @author 张凯大天才
 */
@Service
@Primary
public class FamilyServiceImpl extends ServiceImpl<FamilyMapper, Family> implements IFamilyService {
    @Autowired
    private DictionaryDataMapper dataMapper;


    @Override
    public void insert(FamilyVO family, Integer userId) {
        family.setEmployeeId(userId);
        family.setRelationId(dataMapper.selectArchivesTypes(family.getRelationName(),"家庭关系"));
        LocalDateTime dateTime = LocalDateTime.now();
        family.setGmtCreate(dateTime);
        baseMapper.insert(family);
    }

    @Override
    public void updateById(FamilyVO family) {
        family.setRelationId(dataMapper.selectArchivesTypes(family.getRelationName(),"家庭关系"));
        baseMapper.updateById(family);
    }

    @Override
    public FamilyVO selectById(Integer id) {
        FamilyVO family = baseMapper.selectById(id);
        if (StringUtil.isNull(family)) {
            throw new ServiceException(ResultEnum.roleIsNull);
        }
        return family;
    }

    @Override
    public void datchInsert(List<Family> lists) {
        lists.forEach(list->{
            list.setGmtCreate(LocalDateTime.now());
        });
        baseMapper.batchInsert(lists);
    }
}
